package com.ygxsk.carhome.ui.adapter;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

import android.graphics.drawable.PictureDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.response.HomeMainItemBean;
import com.ygxsk.carhome.svg.SvgSoftwareLayerSetter;
import com.ygxsk.carhome.utils.CommUtils;

import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-17  16:07
 * @Description:档案管理，主页列表适配器
 */
public class HomeMainItemAdapter extends BaseQuickAdapter<HomeMainItemBean, BaseViewHolder> {

    public HomeMainItemAdapter(int layoutResId) {
        super(layoutResId);
    }

    public HomeMainItemAdapter(int layoutResId, @Nullable List<HomeMainItemBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, HomeMainItemBean homeMainItemBean) {

        if (homeMainItemBean != null) {

            //该模块的名称
            String modelName = homeMainItemBean.getPurviewName();

            //该模块的第一级别描述
            String modelFirst = homeMainItemBean.getTotalValue();

            //测试代码
//            String modelSecond = "本日新增100";//homeMainItemBean.getDailyValue();
            //该模块的第二级别描述
            String modelSecond = homeMainItemBean.getDailyValue();

            //该模块的图标信息
            String imageUrl = homeMainItemBean.getPurviewUrl();

            //左侧的帮助布局
            TextView textViewLeftAchieveMent = (TextView) baseViewHolder.getView(R.id.textviewleftachievement);
            //右侧的帮助布局
            TextView textViewRightAchieveMent = (TextView) baseViewHolder.getView(R.id.textviewrightachievement);

            //评估的方式标签
            TextView textVieModelName = (TextView) baseViewHolder.getView(R.id.textviemodelname);

            //第一级别描述
            TextView textVieModelFirstDes = (TextView) baseViewHolder.getView(R.id.textviemodelfirstdes);

            //第二级别描述
            TextView textVieModelSecondDes = (TextView) baseViewHolder.getView(R.id.textviemodelseconddes);

            //该模块的图片信息
            ImageView imageGridInHomeTopItem = (ImageView) baseViewHolder.getView(R.id.imagegridinhometopitem);

            //默认是展示的
//            int currentPosition = getItemPosition(smartGoOutItemBean);
//            if (currentPosition==getData().size()-1){
//                textViewHelpInSmartList.setVisibility(View.GONE);
//            }else{
//                textViewHelpInSmartList.setVisibility(View.VISIBLE);
//            }

            //判断是奇数还是偶数：
            int indexPosition = this.getItemPosition(homeMainItemBean);
            //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "indexPosition-->" + indexPosition);

//            if (indexPosition==0){
//
//                if (textViewLeftAchieveMent != null) {
//                    textViewLeftAchieveMent.setVisibility(View.VISIBLE);
//                }
//
//                if (textViewRightAchieveMent != null) {
//                    textViewRightAchieveMent.setVisibility(View.GONE);
//                }
//
//
//            }else

            if (indexPosition % 2 == 1) {//是偶数

                if (textViewLeftAchieveMent != null) {
                    textViewLeftAchieveMent.setVisibility(View.GONE);
                }

                if (textViewRightAchieveMent != null) {
                    textViewRightAchieveMent.setVisibility(View.VISIBLE);
                }

            } else {//是偶数

                if (textViewLeftAchieveMent != null) {
                    textViewLeftAchieveMent.setVisibility(View.VISIBLE);
                }

                if (textViewRightAchieveMent != null) {
                    textViewRightAchieveMent.setVisibility(View.GONE);
                }

            }

            //模块的名称
            CommUtils.setText(textVieModelName, modelName);
            //模块的一级描述 是总共的
            CommUtils.setText(textVieModelFirstDes, modelFirst);
            //模块的二级描述  是待处理的
            CommUtils.setText(textVieModelSecondDes, modelSecond);

            //加载图片的信息，该模块的图片信息

            if (!TextUtils.isEmpty(imageUrl)) {

                if (imageUrl.endsWith(ConstantSign.SVG_POSTFIX)) {

//                    LogUtils.d(ConstantSign.LOGCAT_NOMAL + imageUrl);

                    Glide.with(getContext())
                            .as(PictureDrawable.class)
                            //.placeholder(R.drawable.icon_errorfang)//原始使用这个占位图
                            .error(R.drawable.icon_errorfang)//
                            .transition(withCrossFade())//
                            .listener(new SvgSoftwareLayerSetter())//
                            .load(imageUrl)//动态取出
                            .into(imageGridInHomeTopItem);//
                } else {

                    Glide.with(getContext())
                            .load(imageUrl)
                            .centerInside()//
                            .error(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                            .placeholder(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                            .fallback(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                            .into(imageGridInHomeTopItem);

                }

            } else {

                //如果图片的加载链接为空
                Glide.with(getContext())
                        .load(imageUrl)
                        .centerInside()//
                        .error(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                        .placeholder(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                        .fallback(ContextCompat.getDrawable(getContext(), R.drawable.icon_errorfang))
                        .into(imageGridInHomeTopItem);

            }
        }
    }
}
